/*
 * Config.java
 * 
 * This file is part of Netuno Proteu.
 * 
 * CajuScript is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3, or (at your option) 
 * any later version.
 * 
 * CajuScript is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with CajuScript.  If not, see <http://www.gnu.org/licenses/>.
*/

package org.netuno.proteu;

import java.util.ArrayList;
import org.netuno.psamata.Values;
import org.netuno.psamata.mail.SendMail;

import java.util.Map;
import java.util.HashMap;
import java.util.List;

/**
 * Configuration.
 * @author eduveks
 */
public final class Config {
    /**
     * Base Folder.
     */
    private static String baseFolder  = "";
    /**
     * Build Folder.
     */
    private static String buildFolder  = "build";
    /**
     * Public Folder.
     */
    private static String publicFolder  = "public";
    /**
     * Cache Folder.
     */
    private static String cacheFolder  = "cache";
    /**
     * Public Folder.
     */
    private static String uploadFolder  = "";
    /**
     * Session is Active?
     */
    private static boolean sessionActive = true;
    /**
     * Session Time Out.
     */
    private static long sessionTimeOut = 900000;
    /**
     * Tag open code block.
     */
    private static String tagOpen  = "<%";
    /**
     * Tag close code block.
     */
    private static String tagClose = "%>";
    /**
     * Script files extension.
     */
    private static Map<String, String> extensions = new HashMap<String, String>();
    /**
     * 
     */
    private static String classExtension = ".proteu";
    /**
     * HTML Error 404.
     */
    private static String error404  = "";
    /**
     * Buffer Size.
     */
    private static int bufferSize = 2048;
    /**
     * Upload limit size.
     */
    private static int formLimit = 10240000;
    /**
     * Mime Types.
     */
    private static String mimeTypesFile = "mime_types.properties";
    /**
     * Character Encoding.
     */
    private static String characterEncoding = "UTF-8";
    /**
     * Language.
     */
    private static String language = "en-us";
    /**
     * All sessions.
     */
    private static Values sessions = new Values();
    /**
     * Config Global.
     */
    private static Values config = new Values();
    /**
     * Mime Types.
     */
    private static Values mimeTypes = new Values();
    /**
     * Is to rebuild?
     */
    private static boolean rebuild = true;
    /**
     * Rebuild restricting to folder.
     */
    private static String rebuildRestrict = "";
    /**
     * Is Starting.
     */
    private static boolean starting = true;
    /**
     * Is to show error detail?
     */
    private static boolean errorDetail = false;
    /**
     * Send error by e-mail
     */
    private static SendMail errorSendMail = null;

    private static List<Cache> cacheEntries = new ArrayList<Cache>();
    /**
     * Configuration.
     */
    private Config() { }
    /**
     * Get Path of the Base Folder.
     * @return base folder
     */
    public static String getBase() {
        return baseFolder;
    }
    /**
     * Set Path of the Base Folder.
     * @param folderPath base folder
     */
    public static void setBase(final String folderPath) {
        baseFolder = folderPath;
    }
    /**
     * Get Path of the Build Folder.
     * @return build folder
     */
    public static String getBuild() {
        return buildFolder;
    }
    /**
     * Set Path of the Build Folder.
     * @param folderPath build folder
     */
    public static void setBuild(final String folderPath) {
        buildFolder = folderPath;
    }
    /**
     * Get Path of the Public Folder.
     * @return public folder
     */
    public static String getPublic() {
        return publicFolder;
    }
    /**
     * Set Path of the Public Folder.
     * @param folderPath public folder
     */
    public static void setPublic(final String folderPath) {
        publicFolder = folderPath;
    }
    /**
     * Get Path of the Upload Folder.
     * @return upload folder
     */
    public static String getUpload() {
        return uploadFolder;
    }
    /**
     * Set Path of the Upload Folder.
     * @param folderPath upload folder
     */
    public static void setUpload(final String folderPath) {
        uploadFolder = folderPath;
    }
    /**
     * Get Path of the Upload Folder.
     * @return upload folder
     */
    public static String getCache() {
        return cacheFolder;
    }
    /**
     * Set Path of the Upload Folder.
     * @param folderPath upload folder
     */
    public static void setCache(final String folderPath) {
        cacheFolder = folderPath;
    }

    public static List<Cache> getCacheEntries() {
        return cacheEntries;
    }

    /**
     * Is enabled or disabled the sessions.
     * @return session active
     */
    public static boolean isSessionActive() {
        return sessionActive;
    }
    /**
     * Set enabled or disabled the sessions.
     * @param active session active
     */
    public static void setSessionActive(final boolean active) {
        sessionActive = active;
    }
    /**
     * Get life time the sessions.
     * @return session time out
     */
    public static long getSessionTimeOut() {
        return sessionTimeOut;
    }
    /**
     * Set life time the sessions.
     * @param time session time out
     */
    public static void setSessionTimeOut(final long time) {
        sessionTimeOut = time;
    }
    /**
     * Get tag of opened for all codes blocks of luajava in files .ljp.
     * @return tag open
     */
    public static String getTagOpen() {
        return tagOpen;
    }
    /**
     * Set tag of opened for all codes blocks of scripts server.
     * @param tag tag open
     */
    public static void setTagOpen(final String tag) {
        tagOpen = tag;
    }
    /**
     * Set tag of closed for all codes blocks of scripts server.
     * @return tag close
     */
    public static String getTagClose() {
        return tagClose;
    }
    /**
     * Set tag of closed for all codes blocks of scripts server.
     * @param tag tag close
     */
    public static void setTagClose(final String tag) {
        tagClose = tag;
    }
    /**
     * Get extension of the CJP files for compatibilites whith editors.
     * Default is .cjp but can use .cjp.htm.
     * @return Extension
     */
    public static Map<String, String> getExtensions() {
        return extensions;
    }
    /**
     * Set extension of the CJP files for compatibilites whith editors.
     * Default is .cjp but can use .cjp.htm
     * @param exts Extensions
     */
    public static void setExtensions(final Map<String, String> exts) {
        extensions = exts;
    }
    /**
     * Get extension of the Classes.
     * Default is .proteu 
     * @return class extension
     */
    public static String getClassExtension() {
        return classExtension;
    }
    /**
     * Set extension of the Classes.
     * Default is .proteu
     * @param classExtension class extension
     */
    public static void setClassExtension(String classExtension) {
        Config.classExtension = classExtension;
    }
    /**
     * Get ERROR 404 - "Not Found" redirect to this address.
     * @return url to error 404
     */
    public static String getError404() {
        return error404;
    }
    /**
     * Set ERROR 404 - "Not Found" redirect to this address.
     * @param url error 404
     */
    public static void setError404(final String url) {
        error404 = url;
    }
    /**
     * Get Buffer for downloading files, default is 15000000 (equals the 15mb).
     * @return buffer size
     */
    public static int getBufferSize() {
        return bufferSize;
    }
    /**
     * Set Buffer for downloading files, default is 15000000 (equals the 15mb).
     * @param size buffer size
     */
    public static void setBufferSize(final int size) {
        bufferSize = size;
    }
    /**
     * Get limit of bytes in all forms, default is 2097152
     * (equals the 2 megabytes).
     * @return time out
     */
    public static int getFormLimit() {
        return formLimit;
    }
    /**
     * Set limit of bytes in all forms, default is 2097152
     * (equals the 2 megabytes).
     * @param limit Upload Limit
     */
    public static void setFormLimit(final int limit) {
        formLimit = limit;
    }
    /**
     * Get path of the Mime Types file, for files content type.
     * @return mime types
     */
    public static String getMimeTypesFile() {
        return mimeTypesFile;
    }
    /**
     * Set path of the Mime Types file, for files content type.
     * @param file mime types
     */
    public static void setMimeTypesFile(final String file) {
        mimeTypesFile = file;
    }
    /**
     * Rebuild
     * @return rebuild
     */
    public static boolean isRebuild() {
        return rebuild;
    }
    /**
     * Rebuild
     * @param rebuild Rebuild
     */
    public static void setRebuild(final boolean rebuild) {
        Config.rebuild = rebuild;
    }
    /**
     * Get restrict folder to rebuild.
     * @return Folder path.
     */
    public static String getRebuildRestrict() {
        return rebuildRestrict;
    }
    /**
     * Set restrict folder to rebuild.
     * @param rebuildRestrict Folder path.
     */
    public static void setRebuildRestrict(String rebuildRestrict) {
        Config.rebuildRestrict = rebuildRestrict;
    }
    /**
     * Get Character Encoding for Url Encode and Decode.
     * @return url character encoding
     */
    public static String getCharacterEncoding() {
        return characterEncoding;
    }
    /**
     * Set Character Encoding for Url Encode and Decode.
     * @param charEncoding Url character encoding
     */
    public static void setCharacterEncoding(final String charEncoding) {
        characterEncoding = charEncoding;
    }
    /**
     * Get Language.
     * @return language
     */
    public static String getLanguage() {
        return language;
    }
    /**
     * Set Language.
     * @param lang
     */
    public static void setLanguage(final String lang) {
        language = lang;
    }
    /**
     * Is the first service.
     * @return starting
     */
    public static boolean isStarting() {
        return starting;
    }
    /**
     * Set if is or not the first service, to rebuild.
     * @param param starting
     */
    public static void setStarting(final boolean param) {
        Config.starting = param;
    }
    /**
     * Is to show error detail?
     * @return error detail actived or not
     */
    public static boolean isErrorDetail() {
        return errorDetail;
    }
    /**
     * Set if is to show error detail?
     * @param param Error detail actived
     */
    public static void setErrorDetail(final boolean param) {
        Config.errorDetail = param;
    }
    /**
     * Get error send mail
     * @return error send mail
     */
    public static SendMail getErrorSendMail() {
		return errorSendMail;
	}
    /**
     * Set error send mail
     * @param param Send mail
     */
	public static void setErrorSendMail(SendMail errorSendMail) {
		Config.errorSendMail = errorSendMail;
	}
	/**
     * Get Sessions.
     * @return sessions
     */
    public static Values getSessions() {
        return sessions;
    }
    /**
     * Get Config.
     * @return config
     */
    public static Values getConfig() {
        return config;
    }
    /**
     * Get Mime Types.
     * @return mime types
     */
    public static Values getMimeTypes() {
        return mimeTypes;
    }
    /**
     * Load Mime Types Values.
     */
    public static void loadMimeTypes() {
        try {
            mimeTypes.loadFile(new java.io.File(mimeTypesFile));
        } catch (Exception e) {
            new Error(e);
        }
    }
}
